<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns=" http://www.w3.org/1999/xhtml">  
<head>  
    <title>Timer</title> 
       <meta meta="utf-8"> 
</head>  
<body>  
    <div id="timer1">  
    </div>  
    <div id="timer2">  
    </div>  
    <div id="timer3">  
    </div>  
</body>  
  
<script type="text/javascript">  
     
    //构造闭包 避免污染全局变量
    var addTimer = function () {     
        var list = [],     
            interval;     

        return function (id, time) {     
            if (!interval)     
                interval = setInterval(go, 1000);  
            var endtime=new Date(time);//结束时间
            var nowtime = new Date();//当前时间
            var lefttime= parseInt( (endtime.getTime() - nowtime.getTime())/1000);    //原为毫秒，除1000转为秒
            list.push({  //将每个更新项目加入数组
            	ele: document.getElementById(id),
            	time: lefttime 
            });     
        }     
    	//用于更新页面上数据
        function go() {      
            for (var i = 0; i < list.length; i++) {     
                list[i].ele.innerHTML = getTimerString(list[i].time ? list[i].time -= 1 : 0);  
                //执行更新时间函数，如果数组里有内容，list[i].time -=1 (将秒数减1)，则返回数组内容，否则返回0   
                if (!list[i].time)     
                    list.splice(i--, 1);  
                    //arrayObj.splice(deletePos,deleteCount); 
                    //删除从指定位置deletePos开始的指定数量
                    //这里用来清空数组内容 
            }     
        }     
    	//将更新的时间返回
        function getTimerString(time) {     
                d = Math.floor(time / 86400),     
                h = Math.floor((time % 86400) / 3600),     
                m = Math.floor(((time % 86400) % 3600) / 60),     
                s = Math.floor(((time % 86400) % 3600) % 60);     
            if (time>0)     
                return d + "天" + h + "小时" + m + "分" + s + "秒";       
            else return "时间到";     
        }     
    } ();     
    addTimer("timer1", "2017/4/15,12:20:12");     
    addTimer("timer2", "2017/5/15,12:20:12");     
    addTimer("timer3", "2017/1/15,12:20:12");  
</script>  
  
</html>
